﻿using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Joker.View.Editor {
    public partial class WarehouseOutDataEdit : Form {
        public int WarehouseOutID = -1;
        public int WarehouseOutDetailID = -1;
        public string sqlstr = "SELECT [WarehouseOutDetail].[ID],[Goods].[Name] [GoodsName],[Amount],[Supply].[Name] [SupplyName] FROM [WarehouseOutDetail] JOIN [Goods] ON [Goods].[ID] = [WarehouseOutDetail].[GoodsID] JOIN [Supply] ON [Supply].[ID] = [WarehouseOutDetail].[SupplyID]";
        public WarehouseOutDataEdit() {
            InitializeComponent();
        }

        private void WarehouseOutDataEdit_Load(object sender, EventArgs e) {
            DataTable WarehouseOutType = DBHelper.GetDataTable("SELECT * FROM [WarehouseOutType]");
            comboBox1.DataSource = WarehouseOutType;
            comboBox1.DisplayMember = "TypeName";
            comboBox1.ValueMember = "ID";
            comboBox1.SelectedIndex = 0;
            DataTable Warehouse = DBHelper.GetDataTable("SELECT [ID],[Name] FROM [Warehouse]");
            comboBox2.DataSource = Warehouse;
            comboBox2.DisplayMember = "Name";
            comboBox2.ValueMember = "ID";
            comboBox2.SelectedIndex = 0;
            DataTable Goods = DBHelper.GetDataTable("SELECT * FROM [Goods]");
            comboBox3.DataSource = Goods;
            comboBox3.DisplayMember = "Name";
            comboBox3.ValueMember = "ID";
            comboBox3.SelectedIndex = 0;
            DataTable Supply = DBHelper.GetDataTable("SELECT [ID],[Name] FROM [Supply]");
            comboBox4.DataSource = Supply;
            comboBox4.DisplayMember = "Name";
            comboBox4.ValueMember = "ID";
            comboBox4.SelectedIndex = 0;
            if (WarehouseOutID != -1) {
                DataTable data = DBHelper.GetDataTable("SELECT * FROM [WarehouseOut]");
                textBox1.Text = WarehouseOutID.ToString();
                comboBox1.SelectedValue = (int)data.Rows[0][1];
                comboBox2.SelectedValue = (int)data.Rows[0][4];
                dateTimePicker1.Value = DateTime.Parse(data.Rows[0][2].ToString());
                textBox2.Text = data.Rows[0][3].ToString().Equals("True") ? "已通过审核" : "未通过审核";
                dataGridView1.DataSource = DBHelper.GetDataTable($"{sqlstr} WHERE [WarehouseOutID] = {WarehouseOutID}");
            }
            comboBox1.SelectedIndexChanged += new System.EventHandler(comboBox1_SelectedIndexChanged);
            comboBox2.SelectedIndexChanged += new System.EventHandler(comboBox2_SelectedIndexChanged);
            dateTimePicker1.ValueChanged += new System.EventHandler(dateTimePicker1_ValueChanged);
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e) {
            WarehouseOutDetailID = (int)dataGridView1.CurrentRow.Cells[0].Value;
            DataTable Detail = DBHelper.GetDataTable("SELECT * FROM [WarehouseOutDetail]");
            //comboBox3.SelectedValue = Detail.Rows[0][]
            button2.Text = "修改";
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) {
            if (dataGridView1.CurrentRow != null) {
                int ID = (int)dataGridView1.CurrentRow.Cells[0].Value;
                if (MessageBox.Show($"你确定要删掉编号为{ID}的数据吗?") == DialogResult.Yes) {
                    if (DBHelper.ExecuteNonQuery($"DELETE [WarehouseOutDetail] WHERE [ID] = {ID}")) {
                        MessageBox.Show("删除成功！");
                    } else {
                        MessageBox.Show("删除失败！");
                    }
                    dataGridView1.DataSource = DBHelper.GetDataTable($"{sqlstr} WHERE [WarehouseOutID] = {WarehouseOutID}");
                }
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
            if (WarehouseOutID != -1) {
                if (DBHelper.ExecuteNonQuery($"UPDATE [WarehouseOut] SET [TypeID] = {comboBox1.SelectedValue} WHERE [ID] = {WarehouseOutID}")) {
                    MessageBox.Show("修改类型成功!");
                } else {
                    MessageBox.Show("修改类型失败!");
                }
            }
        }
        private void dateTimePicker1_ValueChanged(object sender, EventArgs e) {
            if (WarehouseOutID != -1) {
                if (DBHelper.ExecuteNonQuery($"UPDATE [WarehouseOut] SET [Date] = CAST('{dateTimePicker1.Value.ToString("yyyy-MM-dd")}' AS DATE) WHERE [ID] = {WarehouseOutID}")) {
                    MessageBox.Show("修改日期成功!");
                } else {
                    MessageBox.Show("修改日期失败!");
                }
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) {
            if (WarehouseOutID != -1) {
                if (DBHelper.ExecuteNonQuery($"UPDATE [WarehouseOut] SET [WarehouseID] = {comboBox2.SelectedValue} WHERE [ID] = {WarehouseOutID}")) {
                    MessageBox.Show("修改仓库成功!");
                } else {
                    MessageBox.Show("修改仓库失败!");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e) {
            if (WarehouseOutID == -1) {
                if (DBHelper.ExecuteNonQuery($"INSERT [WarehouseOut] VALUES({comboBox1.SelectedValue},CAST('{dateTimePicker1.Value.ToString("yyyy-MM-dd")}' AS DATE),0,{comboBox2.SelectedValue})")) {
                    DataTable table = DBHelper.GetDataTable("SELECT MAX([ID]) FROM [WarehouseOut]");
                    WarehouseOutID = (int)table.Rows[0][0];
                    textBox1.Text = WarehouseOutID.ToString();
                } else {
                    MessageBox.Show("创建入库单失败！");
                    return;
                }
            }
            if (DBHelper.ExecuteNonQuery($"INSERT [WarehouseOutDetail] VALUES({WarehouseOutID},{comboBox3.SelectedValue},{numericUpDown1.Value},{comboBox4.SelectedValue})")) {
                MessageBox.Show("新增成功！");
            } else {
                MessageBox.Show("新增失败！");
            }
            dataGridView1.DataSource = DBHelper.GetDataTable($"{sqlstr} WHERE [WarehouseOutID] = {WarehouseOutID}");
        }
    }
}
